package com.example.demo.test;

import com.example.demo.component.docker.docker_client;
import com.example.demo.component.docker.docker_client_factory;
import com.spotify.docker.client.messages.Container;

import java.util.List;

/**
 * @ClassName main
 * @Description
 * @date 2022/11/7 11:18
 * @Author yanceysong
 * @Version 1.0
 */
public class docker_test {
    public static void main(String[] args) {
        docker_test();
    }

    /**
     * docker 基本使用demo
     * GitHub 文档 https://github.com/spotify/docker-client/blob/master/docs/user_manual.md
     * docker api文档：https://docs.docker.com/engine/api/v1.18/
     */
    private static void docker_test() {
        try {
            System.out.println(Runtime.getRuntime().availableProcessors());
            //获取client
            docker_client docker_client = docker_client_factory.get_docker_client("172.21.2.7", "2375");
//            List<ImageSearchResult> dispatcher = docker_client.search_images_by_local("dispatcher");
//            //拉取镜像
//            docker_client.pull_image("nginx:1.15.5");
//            //容器基础设置
//            create_container_param param = new create_container_param();
//            param.setContainer_name("nginx5");
//            param.setImage_version("1.15.5");
//            param.setImage_name("nginx");
//            //端口号映射配置
//            final String[] ports = {"80/tcp", "22/tcp"};
//            final Map<String, List<PortBinding>> portBindings = new HashMap<>();
//            for (String port : ports) {
//                List<PortBinding> hostPorts = new ArrayList<>();
//                //主机的port
//                hostPorts.add(PortBinding.of("0.0.0.0", "2134"));
//                //host_port是主机port
//                portBindings.put(port, hostPorts);
//            }
//            param.setPortBindings(portBindings);
//            //路径映射配置
//            create_container_param.path_map path_map = new create_container_param.path_map();
//            path_map.setContainer_path("/home");
//            path_map.setRead_only(true);
//            path_map.setHost_path("/data");
//            ArrayList<create_container_param.path_map> list = new ArrayList<>();
//            list.add(path_map);
//            //添加路径映射
//            param.setPath_maps(list);
//            //创建容器
//            ContainerCreation container = docker_client.create_container(param);
//            //开启容器
//            docker_client.start_container(container.id());
//            //关闭容器
//            docker_client.stop_container(container.id());
            List<Container> runtime_containers = docker_client.get_runtime_containers();
            for (Container c: runtime_containers){
                System.out.println(docker_client.get_logs(c.id()));
                System.out.println(docker_client.exec_command(c.id(),""));
            }
            //查看日志
            System.out.println(docker_client.get_logs("247ae9cd1317"));
        } finally {
            docker_client_factory.close("192.168.75.132");
        }
    }
}
